import React from 'react'
const AuthRouter = (props) => {
  return (
    <>
      {
        props.auth && <AuthLogin />
      }
      {props.component}
    </>
  )

}
const AuthLogin = () => {
  let navigate = React.$router.useNavigate()
  const login = async (LoginUser) => {
    let data = {
      mobile: LoginUser.mobile,
      id: LoginUser.id
    }
    let result = await React.$api.checklogin(data)
    if (result) {
      if (result.code == 1) {
        // 存储
        const expires = new Date()
        // 半天
        expires.setDate(Date.now() + 1000 * 60 * 60 * 12)
        React.$cookie.save('User',result.data,{path: '/',expires: expires});
      } else {
        React.$vant.Toast.fail('非法登录，请重新登录')
        React.$cookie.remove('User',{ path: '/' })
        navigate('/business/login')
      }
    }
  }
  React.useEffect(() => {
    var LoginUser = React.$cookie.load('User') ? React.$cookie.load('User') : {}
    if (JSON.stringify(LoginUser) === "{}") {
      React.$vant.Toast.fail('未登录，请登录')
      navigate('/business/login')
    } else {
      login(LoginUser)
    }
  },[])

  return false

}
export default AuthRouter;